MinMaxSliderPropertyDrawer.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace ExternPropertyAttributes.Editor
  4. {
  5. [CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
  6. public class MinMaxSliderPropertyDrawer : PropertyDrawerBase
  7. {
  8. protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
  9. {
  10. return (property.propertyType == SerializedPropertyType.Vector2)
  11. ? GetPropertyHeight(property)
  12. : GetPropertyHeight(property) + GetHelpBoxHeight();
  13. }
  14. protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
  15. {
  16. EditorGUI.BeginProperty(rect, label, property);
  17. MinMaxSliderAttribute minMaxSliderAttribute = (MinMaxSliderAttribute)attribute;
  18. if (property.propertyType == SerializedPropertyType.Vector2)
  19. {
  20. EditorGUI.BeginProperty(rect, label, property);
  21. float indentLength = ExternalCustomEditorGUI.GetIndentLength(rect);
  22. float labelWidth = EditorGUIUtility.labelWidth + ExternalCustomEditorGUI.HorizontalSpacing;
  23. float floatFieldWidth = EditorGUIUtility.fieldWidth;
  24. float sliderWidth = rect.width - labelWidth - 2.0f * floatFieldWidth;
  25. float sliderPadding = 5.0f;
  26. Rect labelRect = new Rect(
  27. rect.x,
  28. rect.y,
  29. labelWidth,
  30. rect.height);
  31. Rect sliderRect = new Rect(
  32. rect.x + labelWidth + floatFieldWidth + sliderPadding - indentLength,
  33. rect.y,
  34. sliderWidth - 2.0f * sliderPadding + indentLength,
  35. rect.height);
  36. Rect minFloatFieldRect = new Rect(
  37. rect.x + labelWidth - indentLength,
  38. rect.y,
  39. floatFieldWidth + indentLength,
  40. rect.height);
  41. Rect maxFloatFieldRect = new Rect(
  42. rect.x + labelWidth + floatFieldWidth + sliderWidth - indentLength,
  43. rect.y,
  44. floatFieldWidth + indentLength,
  45. rect.height);
  46. // Draw the label
  47. EditorGUI.LabelField(labelRect, label.text);
  48. // Draw the slider
  49. EditorGUI.BeginChangeCheck();
  50. Vector2 sliderValue = property.vector2Value;
  51. EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);
  52. sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);
  53. sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));
  54. sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
  55. sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue);
  56. if (EditorGUI.EndChangeCheck())
  57. {
  58. property.vector2Value = sliderValue;
  59. }
  60. EditorGUI.EndProperty();
  61. }
  62. else
  63. {
  64. string message = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields";
  65. DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
  66. }
  67. EditorGUI.EndProperty();
  68. }
  69. }
  70. }